<%--
  Created by IntelliJ IDEA.
  User: 18318
  Date: 2022/7/15
  Time: 15:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common.jsp" %>
<html>
<head>
    <title>SDS测试结果页面</title>
</head>
<body>
<%--头部工具栏--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger " lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<%--行内工具栏--%>
<script type="text/html" id="rowBarDemo">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="check">查看结果</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="result">查看详情</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<%--查看详情的表格--%>
<form class="layui-form" action="" id="addForm" enctype="multipart/form-data" style="display: none">
    <table id="clickResult" lay-filter="test"></table>
</form>

<%--模糊查询--%>
<form class="layui-form" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="name" placeholder="请输入姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">测试计划id</label>
        <div class="layui-input-inline">
            <input type="text" name="testPlanId" placeholder="请输入测试计划id" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="selectFormfilter">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="myButton">重置</button>
        </div>
    </div>
</form>
<style>
    #show {
        /*width: 600px;*/
        /*height: 800px;*/
        /*border: 4px solid cornflowerblue;*/
        margin: auto;

        background: #C2BE9E;
        font-family: 华文行楷;
        font-size: 25px;
        /*绝对定位也行，但是父也不占位。包含块布局的时候一般父相对  父相子绝*/
    }

    #bb {
        width: 200px;
        height: 80px;
        margin: auto;
        line-height: 80px;
    }

    #result {
        width: 400px;
        height: 80px;
        margin: auto;
        line-height: 80px;

    }
</style>
<%--饼图--%>
<div id="show" style="width: 100%;height: 100%; display: none;">
    <div id="bb">SDS测试结果</div>
    <div id="result" style="text-align: center"></div>
    <div style="width: 100%;height: 400px;" id="shopping"></div>
</div>

<%--数据表格--%>
<table id="demo" lay-filter="test"></table>
</body>
<script>
    layui.config({
        version: 1,
        base: 'echarts/' //这个就是你放Echart.js的目录
    }).use(['table', 'jquery', 'form', 'laydate', 'echarts'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery, laydate = layui.laydate, echarts = layui.echarts;

        //第一个实例
        var myTable = table.render({
            elem: '#demo'
            , height: 550
            , url: '${pageContext.request.contextPath}/viewTestResultMht/queryAll.do' //数据接口
            , page: true //开启分页
            , limit: 10
            , limits: [10, 15, 20, 25, 30, 50, 100]
            , toolbar: '#barDemo'
            , cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', width: 70, sort: true}
                    , {field: 'name', title: '姓名', width: 100}
                    , {field: 'phone', title: '电话', width: 120}
                    , {field: 'testPlanId', title: '测试计划ID', width: 120, sort: true}
                    , {field: 'testName', title: '测试名称', width: 120}
                    , {field: 'testerId', title: '测试者id', width: 100}
                    , {field: 'createTime', title: '测试时间', width: 180}
                    , {field: 'resettest', title: '是否重测', width: 100
                       ,templet: function (d) {
                        //得到当前行数据，并拼接成自定义模板
                            if (d.resettest > 7) {
                                //return '可用'
                                return '<a class="layui-btn layui-btn-disabled  layui-btn-xs">需要重测</a>'
                            } else {
                                return '<a class="layui-btn  layui-btn-xs">有效</a>'
                            }

                         }
                      }
                    , {field: 'studyanxiety', title: '学习焦炉', width: 100}
                    , {field: 'peopleanxiety', title: '对人焦虑', width: 100}
                    , {field: 'feelalone', title: '孤独倾向', width: 100}
                    , {field: 'feelremorse', title: '自责倾向', width: 100}
                    , {field: 'feelallergic', title: '过敏倾向', width: 100}
                    , {field: 'body', title: '身体症状', width: 100}
                    , {field: 'feelterror', title: '恐怖倾向', width: 100}
                    , {field: 'easyimpulse', title: '冲动倾向', width: 100}
                    , {fixed: 'right', title: '操作', width: 250, align: 'center', toolbar: '#rowBarDemo'}
                ]
            ]
        });
        /*
        *
        * 行内工具栏
        * */
        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                //console.log(data)
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'check') {
                //layer.msg('查看操作');
                //弹出层
                layer.open({
                    title: '抑郁测试结果图示',
                    type: 1,
                    content: $('#show'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    area: ['600px', '600px']
                });
                $.ajax({
                    url: "${pageContext.request.contextPath}/viewTestResultMht/queryAllById.do",
                    type: "POST",
                    dataType: "JSON",
                    data: {id: obj.data.id},
                    success: function (ret) {
                        var studyanxiety = ret.data.studyanxiety
                        var peopleanxiety = ret.data.peopleanxiety
                        var feelalone = ret.data.feelalone
                        var feelremorse = ret.data.feelremorse
                        var feelallergic = ret.data.feelallergic
                        var body = ret.data.body
                        var feelterror = ret.data.feelterror
                        var easyimpulse = ret.data.easyimpulse
                        var score = studyanxiety+peopleanxiety+feelalone+feelremorse+feelallergic+body+feelterror+easyimpulse;
                        var myChart = echarts.init(document.getElementById('shopping'));

                        var string="你的测试结果为：";
                        if (score>65){
                            string+="你存在一定的心理障碍，请及时找老师沟通"
                        }
                        if (studyanxiety>8){
                            string+="学习焦虑"
                        }
                        if (peopleanxiety>8){
                            string+="、对人焦虑"
                        }
                        if (feelalone>8){
                            string+="、孤独倾向"
                        }
                        if (feelremorse>8){
                            string+="、自责倾向"
                        }
                        if (feelallergic>8){
                            string+="、过敏倾向"
                        }
                        if (body>8){
                            string+="、身体症状"
                        }
                        if (feelterror>8){
                            string+="、恐怖倾向"
                        }
                        if (easyimpulse>8){
                            string+="、冲动倾向"
                        }
                        $("#result").html(string)

                        var option = {
                            legend: {
                                top: 'bottom'
                            },
                            toolbox: {
                                show: true,
                                feature: {
                                    mark: { show: true },
                                    dataView: { show: true, readOnly: false },
                                    restore: { show: true },
                                    saveAsImage: { show: true }
                                }
                            },
                            series: [
                                {
                                    name: 'Nightingale Chart',
                                    type: 'pie',
                                    radius: [50, 100],
                                    center: ['50%', '50%'],
                                    roseType: 'area',
                                    itemStyle: {
                                        borderRadius: 8
                                    },
                                    data: [
                                        { value: studyanxiety, name: '学习焦虑' },
                                        { value: peopleanxiety, name: '对人焦虑' },
                                        { value: feelalone, name: ' 孤独倾向' },
                                        { value: feelremorse, name: ' 自责倾向' },
                                        { value: feelallergic, name: '过敏倾向 ' },
                                        { value: body, name: ' 身体症状' },
                                        { value: feelterror, name: '恐怖倾向 ' },
                                        { value: easyimpulse, name: ' 冲动倾向' }
                                    ]
                                }
                            ]
                        };

                        // 使用刚指定的配置项和数据显示图表。
                        myChart.setOption(option);
                    }
                })

            } else if (obj.event === 'del') {
                layer.confirm('您真的删除行么', function (index) {
                    // 删除用户信息
                    $.ajax({
                        url: '${pageContext.request.contextPath}/viewTestResultMht/delete.do',
                        type: 'POST',
                        dataType: 'JSON',
                        data: {ids: data.id},
                        success: function (data) {
                            if (data.code == 0) {
                                // 关闭对话框
                                layer.closeAll();
                                // 刷新表格数据
                                table.reload('demo');
                            }
                            layer.msg(data.msg);
                        }
                    });
                });
            } else if (obj.event === 'result'){

                //弹出答案框
                layer.open({
                    type: 1
                    , title: '答案'
                    , content: $('#addForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    , offset: '50px'
                    , area: ['800px', '600px']
                });

                table.render({
                    elem: '#clickResult'
                    ,height: 500
                    ,url: '${pageContext.request.contextPath}/viewTestResultMht/queryTestResultById.do?testerId='+data.id //数据接口
                    ,page: true //开启分页
                    , limit: 10
                    , limits: [10, 15, 20, 25, 30, 50, 100]
                    ,cols: [[ //表头
                        {field: 'questionId', title: '题号', width:100, sort: true, fixed: 'left'}
                        ,{field: 'testerId', title: '测试者id', width:120}
                        ,{field: 'question', title: '问题', width:420, sort: true}
                        ,{field: 'answer', title: '回答', width:80
                            ,templet: function (d) {
                                //得到当前行数据，并拼接成自定义模板
                                if (d.answer > 0) {
                                    //return '可用'
                                    return '<p>是</p>'
                                } else {
                                    return '<p>否</p>'
                                }
                            }

                         }
                    ]]
                });


            }
        });
        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        layer.confirm('真的删除选中的行么', function (index) {
                            //obj.del(); //删除对应行（tr）的DOM结构
                            //删除数据库中的数据
                            var idList = [];
                            //遍历数组data，抽取所有的id
                            for (let i = 0; i < data.length; i++) {
                                idList[i] = (data[i].id);
                            }
                            $.ajax({
                                url: "${pageContext.request.contextPath}/viewTestResultMht/delete.do",
                                type: "POST",
                                dataType: "JSON",
                                data: {ids: idList.join(",")},
                                success: function (ret) {
                                    if (ret.code == 0) {
                                        //刷新表格数据
                                        myTable.reload({
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });
                                        layer.msg(ret.msg, {icon: 6});
                                    } else {
                                        layer.msg(ret.msg, {icon: 5});
                                    }
                                }
                            })
                            layer.close(index);
                            //向服务端发送删除指令
                        });
                    }
                    break;
            }
        })

        //================提交条件查询=========
        form.on('submit(selectFormfilter)', function (data) {
            //console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            //提交表单数据
            myTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    name: data.field.name,
                    testPlanId: data.field.testPlanId
                }
                ,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        ///重置条件查询表单，刷新table数据
        $("#myButton").click(function () {
            myTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    name: "",
                    testPlanId: ""
                    //…
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        })
    })
</script>
</html>
